package com.cqyc.imview.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.cqyc.imview.dialog.LoadingDialog
import java.lang.reflect.ParameterizedType


abstract class BaseActivity<T : ViewDataBinding, VM : ViewModel> : AppCompatActivity() {
    protected lateinit var mBinding: T
    protected lateinit var mViewModel: VM
    private lateinit var loadingDialog: LoadingDialog

    abstract fun setLayout(): Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.setContentView(this, setLayout())
        mBinding.lifecycleOwner = this
        mViewModel = ViewModelProvider(this)[getViewModelClass()]
        loadingDialog = LoadingDialog(this)
    }

    private fun getViewModelClass(): Class<VM> {
        val type = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1]
        return type as Class<VM>
    }

    fun showProgress() {
        loadingDialog.show()
    }

    fun dismissProgress() {
        loadingDialog.dismiss()
    }
}